var ws;
var database= window.sessionStorage;
var host = "127.0.0.1";

function ws_init(ip,port){
    if ("WebSocket" in window){
        console.log("您的浏览器支持 WebSocket!");
        ws = new WebSocket("ws://"+ip+":"+port);
        //var ws = new WebSocket("ws://localhost:9001");
        ws.onopen = function(){
            console.log("websocket 已连接上");
        }

        ws.onmessage = function (evt) {
            var dataReceive = evt.data;
            console.log("数据已接收..."+dataReceive);
            return_cmd(dataReceive);
        };

        ws.onclose = function()
        {
            console.log("连接已关闭...");
        };

    }else{
        // 浏览器不支持 WebSocket
        console.log("您的浏览器不支持 WebSocket!");
    }
}


function sdmg(message) {
    if (!window.WebSocket) {
        return;
    }
    if (ws.readyState == WebSocket.OPEN) {
        ws.send(message);
    } else {
        alert("服务器连接失败！");
    }
}

function sendMessage(){
    var dataSend = oWin.val().trim();
    if(dataSend==""|| database.cookies=="" || database.cookies=="获取失败"){
        return;
    }
    if(cmd_config.hasOwnProperty(dataSend)){
        dataSend = cmd_config[dataSend];
        console.log(dataSend);
    }
    var json='{"command":"[cmd]","cookies":"[cookies]"}';
    json=json.replace("[cmd]",dataSend);
    json=json.replace("[cookies]",database.cookies);
    console.log(json);
    ws.send(json);
}
function sendmsg(msg){
    var dataSend = msg.trim();
    console.log(dataSend,cmd_config);
    if(cmd_config.hasOwnProperty(dataSend)){
        dataSend = cmd_config[dataSend];
        console.log(dataSend);
    }
    var json='{"command":"[cmd]","cookies":"[cookies]"}';
    json=json.replace("[cmd]",dataSend);
    json=json.replace("[cookies]",database.cookies);
    console.log(json);
    ws.send(json);
}
function clearmsg(){
    console.log("清屏");
}
function connect(){
    ws_init('118.25.210.158','9002');
}

function disconnect(){
    ws.close();
    console.log('与服务器之间的连接已断开。。。。\n');
}

function login_in(){
    var user = $("input#inputEmail").val();
    var pass = $("input#inputPassword").val();
    var remeber = $("input#remeber")[0].checked;
    database.remeber = remeber;
    if(remeber){
        database.user = user;
        database.pass = pass;
    }
    console.log(remeber)
    if(user.length<6 || pass.length<6){
        console.log(user.length,pass.length);
        alert("错误！请确保qq号和密码大于6位！");
        return;
    }
    var json='{"user":"[user]","pass":"[pass]","command": "B5C7C2BC"}';
    json=json.replace("[user]",user);
    json=json.replace("[pass]",$.md5(pass));
    ws.send(json);
}

function login_out(){
    var user = $("input#inputEmail").val("");
    var pass = $("input#inputPassword").val("");
    database.cookies="";
    console.log("退出登录！");
}

function auto_run()
{
    //console.log('自动下滚');
    var obj = document.getElementById("history");
    obj.scrollTop = obj.scrollHeight;
}

connect();